package com.sias.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.corecode.idgen.WFGIdGenerator;
import com.sias.entity.dto.CourseInfoDto;
import com.sias.entity.pojo.Course;
import com.sias.entity.vo.OneCourseVo;
import com.sias.entity.vo.TwoCourseVo;
import com.sias.mapper.CourseMapper;
import com.sias.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author chf
* @description 针对表【course】的数据库操作Service实现
* @createDate 2024-07-09 22:53:53
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{

    @Autowired
    private WFGIdGenerator wFGIdGenerator;

    @Override
    public String saveCourseInfo(CourseInfoDto courseInfoDto) {
        Course course = new Course();
        BeanUtil.copyProperties(courseInfoDto,course);
        course.setId(String.valueOf(wFGIdGenerator.next()));
        course.setCreateTime(new Date());
        course.setUpdateTime(new Date());
        //只进行了第一步先设置为逻辑删除
        course.setIsDelete("1");
        course.setParentId("0");
        int i = baseMapper.insert(course);
        if (i>0){
            return course.getId();
        }
        return "0";
    }

    @Override
    public List<OneCourseVo> listAll() {
        //最终返回结果
        List<OneCourseVo> allCourseVo=new ArrayList<>();


        //一级分类id数据
        QueryWrapper<Course> courseQueryWrapper=new QueryWrapper<>();
        courseQueryWrapper
                .eq("parent_id","0")
                .eq("is_delete","0");
        //第一层数据
        List<Course> allOneCourse =  baseMapper.selectList(courseQueryWrapper);
        //第二层数据
//        LambdaQueryWrapper<Course> twoCourseQueryWrapper = new LambdaQueryWrapper<>();
//        twoCourseQueryWrapper.eq(Course::getParentId)


        //二级分类以及以下数据
        LambdaQueryWrapper<Course> twoCourseWrapper = new LambdaQueryWrapper<>();
        twoCourseWrapper.ne(Course::getParentId,"0")
                .eq(Course::getIsDelete,"0");
        List<Course> allTwoCourse = baseMapper.selectList(twoCourseWrapper);

        for (int i=0;i<allOneCourse.size();i++){
            Course course=allOneCourse.get(i);
            OneCourseVo oneSubject = new OneCourseVo();
            //第一层
            BeanUtil.copyProperties(course,oneSubject);
            //最终数据
            allCourseVo.add(oneSubject);

            //第二层
            List<TwoCourseVo> twoCourseVos=new ArrayList<>();
            for (int j = 0; j < allTwoCourse.size(); j++) {
                //得到第二层的所有数据便利
                Course twoCourse=allTwoCourse.get(j);
                if (twoCourse.getParentId().equals(course.getId())){
                    TwoCourseVo twoCourseVo = new TwoCourseVo();
                    BeanUtil.copyProperties(twoCourse,twoCourseVo);
                    twoCourseVos.add(twoCourseVo);
                }
            }
            oneSubject.setChildren(twoCourseVos);
        }


       // 填充vo对象
        return allCourseVo;
    }
}




